package org.zky.modules.account.service.impl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zky.modules.account.domain.ZkyAccountRoleTab;
import org.zky.modules.account.repository.ZkyAccountRoleTabRepository;
import org.zky.modules.account.service.ZkyAccountRoleTabService;
import org.zky.modules.account.service.dto.ZkyAccountRoleTabDto;
import org.zky.modules.account.service.dto.ZkyAccountRoleTabQueryCriteria;
import org.zky.modules.account.service.mapper.ZkyAccountRoleTabMapper;
import org.zky.utils.FileUtil;
import org.zky.utils.PageUtil;
import org.zky.utils.QueryHelp;
import org.zky.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author chenglong
* @date 2020-03-23
*/
@Service
//@CacheConfig(cacheNames = "zkyAccountRoleTab")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ZkyAccountRoleTabServiceImpl implements ZkyAccountRoleTabService {

    private final ZkyAccountRoleTabRepository zkyAccountRoleTabRepository;

    private final ZkyAccountRoleTabMapper zkyAccountRoleTabMapper;

    public ZkyAccountRoleTabServiceImpl(ZkyAccountRoleTabRepository zkyAccountRoleTabRepository, ZkyAccountRoleTabMapper zkyAccountRoleTabMapper) {
        this.zkyAccountRoleTabRepository = zkyAccountRoleTabRepository;
        this.zkyAccountRoleTabMapper = zkyAccountRoleTabMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ZkyAccountRoleTabQueryCriteria criteria, Pageable pageable){
        Page<ZkyAccountRoleTab> page = zkyAccountRoleTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zkyAccountRoleTabMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ZkyAccountRoleTabDto> queryAll(ZkyAccountRoleTabQueryCriteria criteria){
        return zkyAccountRoleTabMapper.toDto(zkyAccountRoleTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ZkyAccountRoleTabDto findById(Integer deleteAt) {
        ZkyAccountRoleTab zkyAccountRoleTab = zkyAccountRoleTabRepository.findById(deleteAt).orElseGet(ZkyAccountRoleTab::new);
        ValidationUtil.isNull(zkyAccountRoleTab.getDeleteAt(),"ZkyAccountRoleTab","deleteAt",deleteAt);
        return zkyAccountRoleTabMapper.toDto(zkyAccountRoleTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ZkyAccountRoleTabDto create(ZkyAccountRoleTab resources) {
        return zkyAccountRoleTabMapper.toDto(zkyAccountRoleTabRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ZkyAccountRoleTab resources) {
        ZkyAccountRoleTab zkyAccountRoleTab = zkyAccountRoleTabRepository.findById(resources.getDeleteAt()).orElseGet(ZkyAccountRoleTab::new);
        ValidationUtil.isNull( zkyAccountRoleTab.getDeleteAt(),"ZkyAccountRoleTab","id",resources.getDeleteAt());
        zkyAccountRoleTab.copy(resources);
        zkyAccountRoleTabRepository.save(zkyAccountRoleTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer deleteAt : ids) {
            zkyAccountRoleTabRepository.deleteById(deleteAt);
        }
    }

    @Override
    public void download(List<ZkyAccountRoleTabDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZkyAccountRoleTabDto zkyAccountRoleTab : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" createTime",  zkyAccountRoleTab.getCreateTime());
            map.put(" updateTime",  zkyAccountRoleTab.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}